//
//  WelcomeViewController.swift
//  weibo
//
//  Created by yellow on 15/10/19.
//  Copyright © 2015年 yellow. All rights reserved.
//

import UIKit
import SDWebImage

//显示玩用户头像跳动动画就使用通知让keyWindow改变rootViewController为主界面
class WelcomeViewController: UIViewController {
    
    var viewModel = UserAccountViewModel.sharedViewModel
    
    private lazy var backImage:UIImageView = UIImageView(imageName: "ad_background")
    private lazy var iconImage:UIImageView = {
        let iconImage = UIImageView()
        //设置圆角图片
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        return iconImage
    }()
    private lazy var welcomeLable:UILabel = {
        let lable = UILabel()
        lable.text = "欢迎回来"
        lable.font = UIFont.systemFontOfSize(13)
        lable.textAlignment = NSTextAlignment.Center
        lable.textColor = UIColor.darkGrayColor()
        lable.sizeToFit()
        return lable
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        iconImage.sd_setImageWithURL(viewModel.userIconURL, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    
    //要执行动画，前提就是有view已经显示了，所以在viewDidAppear中编写动画人物
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //自动布局更新布局在snp_updateConstraints中编写，默认情况是在一轮事件循环完后，再更新布局约束
        //如需要强制更新布局，使用view.layoutIfNeeded()
        //注意点，对一个控件不要重复定义约束！！！
        iconImage.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view.snp_top).offset(350)
        }
        welcomeLable.alpha = 0
        
        UIView.animateWithDuration(2.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.welcomeLable.alpha = 1
                    }, completion: { (_) -> Void in
                        //延迟1秒后发送切面界面通知
                        delayDo(1, call: { () -> () in
                            
                            NSNotificationCenter.defaultCenter().postNotificationName(jump2MainVCNotification, object: "MainViewController")
                        })
                })
                
                
                
        }
        
    }

}

//页面布局
extension WelcomeViewController{

    func setUI(){
        view.addSubview(backImage)
        view.addSubview(iconImage)
        view.addSubview(welcomeLable)
        
        //自动布局
        backImage.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.view.snp_left)
            make.top.equalTo(self.view.snp_top)
        }

        //头像居于view的中上部
        iconImage.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(90)
            make.height.equalTo(90)
            make.centerX.equalTo(self.view.snp_centerX)
            make.top.equalTo(self.view.snp_top).offset(100)
        }
        
        welcomeLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.top.equalTo(iconImage.snp_bottom).offset(30)
        }
    }
}
